<?php

namespace app\store\model;

use think\Model;
use think\model\concern\SoftDelete;

class GroupGoods extends Model
{   
    use SoftDelete;

    protected $defaultSoftDelete = 'int';

    protected $autoWriteTimestamp = 'int';

    protected $schema = [
        'id' => 'int',
        'store_id' => 'int',
        'sort' => 'int',
        'spec_type' => 'int',
        'goods_name' => 'string',
        'main_image' => 'string',
        'goods_images' => 'json',
        'goods_attr' => 'json',
        'is_single_buy' => 'int',
        'market_price' => 'float',
        'sell_price' => 'float',
        'group_price' => 'float',
        'group_num' => 'int',
        'virtual_sale' => 'int',
        'total_sale' => 'int',
        'stock' => 'int',
        'total_stock' => 'int',
        'status' => 'int',
        'create_time' => 'int',
        'update_time' => 'int',
        'delete_time' => 'int',
    ];

    protected $readonly = ['goods_attr'];

    protected $type = [
        'goods_images' => 'json',
    ];
    
    public function goodsContent()
    {
        return $this->hasOne(GroupGoodsContent::class);
    }

    public function items()
    {
        return $this->hasMany(GroupGoodsItem::class);
    }

    public function activity()
    {
        return $this->belongsTo(GroupActivity::class);
    }

    /**
     * 商品名称搜索器
     * */
    public function searchGoodsNameAttr($query, $value, $data)
    {
        $query->where('goods_name','like', '%' .$value . '%');
    }

}